use warnings;
use strict;
use WWW::Mechanize;
our $year=2010;
our $dosidcount;
our $dosidtotal=0;
# Initialisierung der Ausgabedateien, Vorhandensein der Ordner wird vorausgesetzt.
#open SEARCH, ">D:/prelex2013/searchresults/searchresults.txt" or die "$!";
#close (SEARCH);
open (DOSID, ">D:/prelex2013/searchresults/dosid5.dat");
close(DOSID);
open CHECK, "> D:/prelex2013/searchresults/check3.dat" or die "$!";
print CHECK "Year\tDOSIDs\n";
close (CHECK);
open LOG, "> D:/prelex2013/searchresults/log3.dat" or die "$!";
close (LOG);
open SEARCHLOCATION, "> D:/prelex2013/searchlocation4.txt" or die "$!";
close SEARCHLOCATION;
while ($year<2014) {
     my $agent = WWW::Mechanize->new();
     $agent->get("http://ec.europa.eu/prelex/rech_simple.cfm?CL=en");
     $agent->form_name("RECHERCHE"); 
     $agent->field("clef2", $year);
     $agent->field("clef1", 'COM');
     $agent->field("nbr_element", '99');
     $agent->click();
     my $initialresult=$agent->content();
     my $residual=$initialresult;
     our $pcpcount=1;
     while ($residual) {
         if ($residual=~qr/liste_resultats\.cfm\?PCP/) {
             $residual=$';
             $pcpcount++;
             print "$pcpcount\n";
            }
         else {
             $residual='';
            }
     }
     $pcpcount=($pcpcount/2)+1;
     if ($pcpcount>1.5) {
         my @pages=(1...$pcpcount);
         foreach my $page(@pages) {
             my $result=$agent->content();
             print "Writing search results for $year, page $page\n";
             open RESULT, "> D:/prelex2013/searchresults/$year/page$page.html" or die "$!";
             print RESULT $result;
             close (RESULT);
             open SEARCHLOCATION, ">> D:/prelex2013/searchlocation4.txt" or die "$!";
             print SEARCHLOCATION "D:/prelex2013/searchresults/$year/page$page.html\n";
             close SEARCHLOCATION;
             open INPUT, "< D:/prelex2013/searchresults/$year/page$page.html" or die "$!";
             my @input=<INPUT>;
             close (INPUT);
             # DOSID AUSLESEN
             $dosidcount=0;
             foreach my $input (@input) {
                 if ($input=~/DosID=(.{6})/) {
                     my $dosid=$1;
                     if ($dosid=~'"') {
                         chop($dosid);
                        }
                     if ($dosidcount=~ /[02468]$/ ) {
                         open (DOSID, ">>D:/prelex2013/searchresults/dosid5.dat");
                         print DOSID "$dosid\n";
                         close(DOSID);                         
                        }
                    $dosidcount++;
                    }
                }
             $dosidtotal=$dosidtotal+($dosidcount/2);
             $agent->follow_link(url_regex => qr/liste_resultats\.cfm\?PCP/, n=>$page);
             $page++;}
        }
     else {
         my $page=1;
         my $result=$agent->content();
         print "Writing search results for $year, page $page\n";
         open RESULT, "> D:/prelex2013/searchresults/$year/page$page.html" or die "$!";
         print RESULT $result;
         close (RESULT);
         open SEARCHLOCATION, ">> D:/prelex2013/searchlocation4.txt" or die "$!";
         print SEARCHLOCATION "D:/prelex2013/searchresults/$year/page$page.html\n";
         close SEARCHLOCATION;
         open INPUT, "< D:/prelex2013/searchresults/$year/page$page.html" or die "$!";
         my @input=<INPUT>;
         close (INPUT);
         $dosidcount=0;
         foreach my $input (@input) {
             if ($input=~/DosID=(.{6})/) {
                 my $dosid=$1;
                 if ($dosid=~'"') {
                     chop($dosid);
                    }
                 if ($dosidcount=~ /[02468]$/ ) {
                     open (DOSID, ">>D:/prelex2013/searchresults/dosid5.dat");
                     print DOSID "$dosid\n";
                     close(DOSID);
                     open LOG, ">> D:/prelex2013/searchresults/log3.dat" or die "$!";
                     print LOG "$dosid\n";
                     close (LOG);
                    }
                $dosidcount++;
                }
            }
         $dosidtotal=$dosidtotal+($dosidcount/2);
        }
    $dosidcount=$dosidcount/2;
    open CHECK, ">> D:/prelex2013/searchresults/check3.dat" or die "$!";
    print CHECK "$year\t$dosidcount\n";
    close (CHECK);
    close (CHECK);
    open LOG, ">> D:/prelex2013/searchresults/log3.dat" or die "$!";
    print LOG "\nYear\t$year\tDOSIDs\t$dosidcount\n\n";
    close (LOG);
    $year++;
    }
